import json


class CourseTable:
    def __init__(self, ctId: int, name: str, setting: "Setting"):
        self.ctId = ctId
        self.name = name
        self.setting = setting  # Setting对象
        self.sourceName = "course-app-miui"
    def to_json(self):
        data = {
            "ctId": self.ctId,
            "name": self.name,
            "setting": self.setting.to_dict(),
            "sourceName": "course-app-miui"
        }
        return json.dumps(data)

    @staticmethod
    def from_timer(data: dict, table_id: int, name: str, table: dict):
        setting = Setting(totalWeek=data["totalWeek"],
                          startSemester=data["startSemester"],
                          weekStart=table["setting"]["weekStart"],
                          morningNum=data["forenoon"],
                          afternoonNum=data["afternoon"],
                          nightNum=data["night"],
                          sections=[Section(i + 1, s["startTime"], s["endTime"]) for i, s in enumerate(data["sections"])],
                          extend=table["setting"]["extend"],
                          speak=table["setting"]["speak"],
                          isWeekend=table["setting"]["isWeekend"],
                          presentWeek=table["setting"]["presentWeek"],
                          id=table["setting"]["id"],
                          )
        return CourseTable(table_id, name, setting)

class Setting:
    def __init__(self, id: int, presentWeek: int, totalWeek: int, isWeekend: 1|0,
                 morningNum: int, afternoonNum: int, nightNum: int, speak: 1|0,
                 weekStart, extend, startSemester: str, sections: list["Section"], school: str = "{}"):
        self.id = id
        self.presentWeek = presentWeek
        self.totalWeek = totalWeek
        self.isWeekend = isWeekend
        self.morningNum = morningNum
        self.afternoonNum = afternoonNum
        self.nightNum = nightNum
        self.speak = speak
        self.weekStart = weekStart
        self.extend = extend          # 字典（需自行解析JSON字符串）
        self.startSemester = startSemester
        self.school = school          # 字典（需自行解析JSON字符串）
        self.sections = sections      # Section对象列表
    def to_dict(self):
        data = {
            "id": self.id,
            "presentWeek": self.presentWeek,
            "totalWeek": self.totalWeek,
            "isWeekend": self.isWeekend,
            "morningNum": self.morningNum,
            "afternoonNum": self.afternoonNum,
            "nightNum": self.nightNum,
            "speak": self.speak,
            "weekStart": self.weekStart,
            "extend": self.extend.to_json() if isinstance(self.extend, Extend) else self.extend,
            "startSemester": self.startSemester,
            "school": self.school,
            "sections": json.dumps([s.to_dict() for s in self.sections])
        }
        return data

class Section:
    def __init__(self, i, s, e):
        self.i = i  # 节次序号
        self.s = s  # 开始时间
        self.e = e  # 结束时间
    def to_json(self):
        data = {
            "i": self.i,
            "s": self.s,
            "e": self.e
        }
        return json.dumps(data)

    def to_dict(self):
        data = {
            "i": self.i,
            "s": self.s,
            "e": self.e
        }
        return data


class Extend:
    def __init__(self, startSemester: int, degree: str, showNotInWeek: bool, bgSetting: "BgSetting"):
        self.startSemester = startSemester
        self.degree = degree
        self.showNotInWeek = showNotInWeek
        self.bgSetting = bgSetting  # BgSetting对象
    def to_json(self):
        data = {
            "startSemester": self.startSemester,
            "degree": self.degree,
            "showNotInWeek": self.showNotInWeek,
            "bgSetting": self.bgSetting.to_json()
        }

class BgSetting:
    def __init__(self, name: str, opacity: 1|0):
        self.name = name
        self.opacity = opacity

    def to_json(self):
        data = {
            "name": self.name,
            "opacity": self.opacity
        }
        return json.dumps(data)